package xiyuan.view.distance;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TongYiCi {
	public static Map <String, String> map;
	public static int size;
	public static Object[] keyArray;
	
	public static String getRandomWord(){
		return (String) keyArray[(int) (Math.random()*size)];
	}

	public static boolean isSynonym(String var1, String var2){
		//比较两个type字符串，找到类的大小，类里面词语的个数=distance
		if (map.get(var1) == null || map.get(var2) == null)
			return false;
		String[] typeList1 = map.get(var1).split("~"),
		typeList2 = map.get(var2).split("~");
		List<Integer> distanceList = new ArrayList<Integer>();
		
		for (String type1:typeList1)
			for (String type2:typeList2)
				if (compare(type1, type2) >= 5)
					return true;
		return false;
	}
	
	static{
		File file = new File("tongyici.txt");	//tongyici.txt位置
		map = new HashMap<String, String>();
		
		if (file.exists()) {
			System.out.println("file found.");
			try {
				BufferedReader cin = new BufferedReader(new InputStreamReader(
						new FileInputStream(file),"UTF-8"));
				String s = cin.readLine();
				int i = 0;
				
				while (s!=null){
					String elements[] = s.split(" ");
					String type = elements[0];	//“= ”代表“相等”、“同义”。末尾的“#”代表“不等”、“同
					//类”，属于相关词语。末尾的“@”代表“自我封闭”、“独立”
					//，它在词典中既没有同义词，也没有相关词。
					
					for (i = 1; i < elements.length; i++){
						String str = elements[i];
						if (map.containsKey(str))
							map.put(str, map.get(str)+"~"+type);
			            else
			            	map.put(str, type);
					}//将文件内容放入map
					
					s = cin.readLine();
				}
	            cin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		size = map.size();
		keyArray = map.keySet().toArray();
	}
	
	public static int compare(String var1, String var2){
		int i = 0;
		for (i = 0; i < 8; i++){
			if (var1.charAt(i) != var2.charAt(i))
				break;
		}
		switch(i){
		case 3:i=2;break;
		case 6:i=5;break;
		}
		return i;
	}
}